package com.hmkj.quartz.queue;

import com.alibaba.fastjson.JSONObject;
import com.hmkj.boot.utils.SpringContextUtils;
import com.hmkj.common.utils.DateUtils;
import com.hmkj.core.constant.CacheID;
import com.hmkj.core.eolinker.endpoints.QuotationEndpoint;
import com.hmkj.core.eolinker.models.ModelRealTime;
import com.hmkj.core.po.account.Account;
import com.hmkj.core.po.account.AccountLog;
import com.hmkj.core.po.order.Order;
import com.hmkj.core.service.account.AccountLogService;
import com.hmkj.core.service.account.AccountService;
import com.hmkj.core.service.cache.RedisService;
import com.hmkj.core.service.order.OrderOperationService;
import com.hmkj.core.service.order.OrderService;
import lombok.extern.slf4j.Slf4j;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;

/**
 * 流单操作队列线程
 *
 * @author eproo
 * @date 2017年03月16日 21:38
 */
@Slf4j
public class OrderQueueThread implements Runnable {


    private RedisService redisService = SpringContextUtils.getBean(RedisService.class);
    private OrderService orderService = SpringContextUtils.getBean(OrderService.class);
    private AccountService accountService = SpringContextUtils.getBean(AccountService.class);
    private AccountLogService accountLogService = SpringContextUtils.getBean(AccountLogService.class);
    private OrderOperationService orderOperationService = SpringContextUtils.getBean(OrderOperationService.class);

    @Override
    public void run() {
        boolean isFinish = true;
        while (!Thread.currentThread().isInterrupted()) {
            try {
                if (!startStopLoss()) {
                    isFinish = false;
                }
                if (!startOrderFlow()) {
                    isFinish = false;
                }
            } catch (Exception e) {
                log.error(e.getMessage());
                e.printStackTrace();
            }
            if (isFinish) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    log.error(e.getMessage());
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 止损平仓队列
     *
     * @return
     */
    private boolean startStopLoss() throws Exception {
        boolean isFinish = false;
        JSONObject obj = redisService.pop(CacheID.STOP_LOSS, JSONObject.class);
        if (null == obj || null == obj.getInteger("orderId")) {
            return true;
        }
        Integer orderId = obj.getInteger("orderId");
        orderService.doCoverOrder(orderId,Order.PINGCANGTYPE.T2.code);
        return isFinish;
    }

    /**
     * 流单队列
     *
     * @param
     */
    private boolean startOrderFlow() throws Exception {
        boolean isFinish = false;
        JSONObject obj = redisService.pop(CacheID.QUEUE_FLOW, JSONObject.class);
        if (null == obj || null == obj.getInteger("orderId")) {
            return true;
        }
        Integer orderId = obj.getInteger("orderId");
        orderService.doFlowOrder(orderId,null);
        return isFinish;
    }
}
